package com.tgy.MyFlowable.workflow.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.core.common.util.StringUtil;
import com.ctsi.core.database.entity.Search;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.core.workflow.enums.EventNodeTypeEnum;
import com.ctsi.workflow.entity.FlwEvent;
import com.ctsi.workflow.mapper.FlwEventMapper;
import com.ctsi.workflow.service.IFlwEventService;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流程事件配置表 服务实现类
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 19:33
 */
@Service
public class FlwEventServiceImpl extends ServiceImpl<FlwEventMapper, FlwEvent> implements IFlwEventService {

    @Override
    public IPage<FlwEvent> listPage(Search search) {
        LambdaQueryWrapper<FlwEvent> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtil.isNotBlank(search.getKeyword())) {
            queryWrapper.like(FlwEvent::getId, search.getKeyword());
        }
        queryWrapper.orderByDesc(FlwEvent::getCreateTime);
        return this.baseMapper.selectPage(PageUtil.getPage(search), queryWrapper);
    }

    @Override
    public List<String> getScript(String processDefinitionId, String actId, String eventType) {
        if (ObjectUtil.isAllNotEmpty(processDefinitionId, eventType)) {
            //查询全局配置的事件类型
            LambdaQueryWrapper<FlwEvent> queryWrapperGlobal = new LambdaQueryWrapper<>();
            queryWrapperGlobal.eq(FlwEvent::getProcessDefinitionId, processDefinitionId)
                    .eq(FlwEvent::getNodeType, EventNodeTypeEnum.GLOBAL.getCode())
                    .eq(FlwEvent::getType, eventType);
            List<FlwEvent> flwEventList = this.list(queryWrapperGlobal);

            if (ObjectUtil.isNotEmpty(actId)) {
                //查询节点配置的事件类型，且节点为该节点的
                LambdaQueryWrapper<FlwEvent> queryWrapperAct = new LambdaQueryWrapper<>();
                queryWrapperAct.eq(FlwEvent::getProcessDefinitionId, processDefinitionId)
                        .eq(FlwEvent::getNodeType, EventNodeTypeEnum.NODE.getCode())
                        .eq(FlwEvent::getActId, actId)
                        .eq(FlwEvent::getType, eventType);
                List<FlwEvent> flowableEventListAct = this.list(queryWrapperAct);
                //合并到一起
                flwEventList.addAll(flowableEventListAct);
            }
            //根据脚本执行顺序排序，并取出脚本内容
            List<String> resultList = flwEventList.stream()
                    .sorted(Comparator.comparing(FlwEvent::getExecSort))
                    .map(FlwEvent::getScript)
                    .collect(Collectors.toList());
            return resultList;
        }
        return null;
    }

    @Override
    public void delete(String id) {
        this.remove(Wrappers.<FlwEvent>lambdaQuery().eq(FlwEvent::getProcessDefinitionId, id));
    }
}

